package com.ruoyi.APPMS.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.APPMS.domain.Logistics;
import com.ruoyi.APPMS.domain.Part;
import com.ruoyi.APPMS.domain.Vehicle;
import com.ruoyi.APPMS.mapper.LogisticsMapper;
import com.ruoyi.APPMS.mapper.PartMapper;
import com.ruoyi.APPMS.mapper.VehicleMapper;
import com.ruoyi.APPMS.service.ILogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 物流管理Service业务层处理
 * 
 * @author tianq
 * @date 2023-07-03
 */
@Service
public class LogisticsServiceImpl extends ServiceImpl<LogisticsMapper,Logistics> implements ILogisticsService {

    @Autowired
    private LogisticsMapper logisticsMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private PartMapper partMapper;
    @Override
    public List<Logistics> do_sign(List<Logistics> to_sign) {
        // 返回签收成功列表
        List<Logistics> sign_success = new ArrayList<Logistics>();
        for (Logistics logistic:to_sign) {
            if (logistic.getVehicleid()!=null&&logistic.getPartid()==null) {
                Vehicle vehicle = vehicleMapper.selectById(logistic.getVehicleid());
                if (vehicle==null) continue;
                vehicle.setStatus(vehicle.getStatus()+1);
                vehicleMapper.updateById(vehicle);
                logistic.setStatus(logistic.getStatus()+1);
                logistic.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                logistic.setDeliverytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                logisticsMapper.updateById(logistic);

                sign_success.add(logistic);
            }
            if (logistic.getVehicleid()==null&&logistic.getPartid()!=null) {
                Part part = partMapper.selectById(logistic.getPartid());
                if (part==null) continue;
                part.setStatus(part.getStatus()+1);
                partMapper.updateById(part);
                logistic.setStatus(logistic.getStatus()+1);
                logistic.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                logistic.setDeliverytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                logisticsMapper.updateById(logistic);

                sign_success.add(logistic);
            }

        }
        return sign_success;
    }
}
